package edu.bit.water.service;

import edu.bit.water.dao.MetaQSDEsubDao;
import edu.bit.water.entity.EntityWaterCount;
import edu.bit.water.entity.MetaQSDEsub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * MetaStandardController的service层
 * author: Page
 */
@Service
public class MetaQSDEsubService {

    @Autowired
    private MetaQSDEsubDao metaQSDEsubDao;

    /**
     * 查询MetaStandard
     */
    public List<MetaQSDEsub> queryQSDEsub(String stNumber) {
        return metaQSDEsubDao.queryQSDEsub(stNumber);
    }


    /**
     * 插入新的metaQSDEsub
     */
    public Map<String, Object> insertMetaQSDEsubService(MetaQSDEsub metaQSDEsub) {
        //metaQSDEsub.dateChange();
        Map<String, Object> result = new HashMap<>();
        Integer num = metaQSDEsubDao.getMaxId();
//        System.out.println(num);
        if (num != null) {
            metaQSDEsub.setId(num + 1);
        }
        else {
            metaQSDEsub.setId(0);
        }
        int tmp = metaQSDEsubDao.insertMetaQSDEsub(metaQSDEsub);
        if (tmp == 1) {
            result.put("status", "success");
        }else {
            result.put("status", "failed");
        }
        return result;
    }


    /**
     * 编辑提交
     */
    public Map<String, Object> updateMetaQSDEsubService(MetaQSDEsub metaQSDEsub) {
        //metaQSDEsub.dateChange();
        Map<String, Object> result = new HashMap<>();
        int tmp = metaQSDEsubDao.update(metaQSDEsub);
        if(tmp == 0) {
            result.put("status", "failed");
        }
        else {
            result.put("status", "success");
        }
        return result;
    }

    /**
     * 级联修改
     */
    public Map<String, Object> waterCountUpdate(EntityWaterCount entityWaterCount) {
        Map<String, Object> result = new HashMap<>();
        int tmp = metaQSDEsubDao.waterCountUpdate(entityWaterCount);
        if(tmp == 0) {
            result.put("status", "failed");
        }
        else {
            result.put("status", "success");
        }
        return result;
    }

    /**
     * 删除metaQSDEsub
     * author： page
     */
    public Map<String, Object> deleteMetaQSDEsub(List<String> metaQSDEsub) {
        Map<String, Object> tmpMap = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < metaQSDEsub.size(); i++) {
            int result = metaQSDEsubDao.delete(Integer.parseInt(metaQSDEsub.get(i)));
            if(result == 0) {
                tmpMap.put(metaQSDEsub.get(i), "failed");
                flag = false;
            }
            else {
                tmpMap.put(metaQSDEsub.get(i), "success");
            }
        }
        if (flag) {
            tmpMap.put("status", "success");
        }
        else {
            tmpMap.put("status", "failed");
        }
        return tmpMap;
    }

    //级联删除
    public Map<String, Object> waterCountDelete(List<String> tempData) {
        Map<String, Object> result = new HashMap<>();
        boolean flag = true;
        String standardNumber = tempData.get(tempData.size() -1);//最后一个数据是标准号
        for (int i = 0; i < tempData.size() - 1; i++) {
            int tmp = metaQSDEsubDao.waterCountDelete(tempData.get(i).toString(), standardNumber);
            if (tmp == 1) {
                result.put(tempData.get(i).toString(), "success");
            }
            else {
                result.put(tempData.get(i).toString(), "failed");
                flag = false;
            }
        }
        if (flag) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
}
